# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : system_oauth_models.py
# Time       ：2023/7/9 10:27
# Author     ：Y-aong
# version    ：python 3.7
# Description：
    用户权限模型类
    user permission model

user——group  多对多
dept ——user 一对多
role ——permission  多对多
role ——user 多对多
group ——permission  多对多

user role  permission  group  dept
user group relation
role permission relation
group permission relation
user role relation

"""
from public.base_model import Base, db
from public.api_utils.jwt_utils import encrypt_password


class SystemUser(Base):
    __tablename__ = 'system_user'

    username = db.Column(db.String(64), unique=True, comment='用户名')
    email = db.Column(db.String(128), unique=True, nullable=True, comment='用户邮箱')
    phone = db.Column(db.String(128), unique=True, nullable=True, comment='用户电话')
    password = db.Column(db.String(255), comment='用户密码')
    img_url = db.Column(
        db.Text,
        default='https://tupian.qqw21.com/article/UploadPic/2020-8/20208522181014944.jpg',
        comment='用户图片链接')
    group_owner = db.Column(db.Boolean, default=False, comment='是否为组所有者')
    dept_id = db.Column(db.Integer, db.ForeignKey('system_department.id'), comment='部门ID')

    @staticmethod
    def check_password(password, login_type, login_value):
        _password = encrypt_password(password)
        if not hasattr(SystemUser, login_type):
            raise ValueError(f'login_type::{login_type} 不存在')
        user = db.session.query(SystemUser).filter(getattr(SystemUser, login_type) == login_value).first()
        if not user:
            raise ValueError(f'用户 {login_value} 不存在')

        db_password = user.password
        if db_password != _password:
            raise ValueError(f'用户名或密码错误')


class SystemGroup(Base):
    __tablename__ = 'system_group'

    group_name = db.Column(db.String(128), unique=True, comment='组名')
    desc = db.Column(db.String(255), comment='组描述')
    owner_id = db.Column(db.Integer, comment='所有者ID')
    owner_name = db.Column(db.String(64), comment='所有者姓名')


class SystemUserGroupRelation(Base):
    __tablename__ = 'system_user_group_relation'

    group_id = db.Column(db.Integer, comment='组ID')
    user_id = db.Column(db.Integer, comment='用户ID')


class SystemPermission(Base):
    __tablename__ = 'system_permission'

    name = db.Column(db.String(128), comment='权限名称')
    menu = db.Column(db.Boolean, default=False, comment='是否菜单，True: 菜单, False: API')
    method = db.Column(db.Enum('GET', 'POST', 'PUT', 'DELETE'), comment='请求方法')
    path = db.Column(db.String(128), comment='请求路径正则')
    pid = db.Column(db.Integer, comment='权限父ID')
    desc = db.Column(db.String(255), comment='权限描述')


class SystemGroupPermissionRelation(Base):
    __tablename__ = 'system_group_permission_relation'

    group_id = db.Column(db.Integer, comment='组ID')
    permission_id = db.Column(db.Integer, comment='权限ID')


class SystemRole(Base):
    __tablename__ = 'system_role'

    role_name = db.Column(db.String(128), comment='角色名称')
    desc = db.Column(db.String(255), comment='角色描述')


class SystemUserRoleRelation(Base):
    __tablename__ = 'system_user_role_relation'

    user_id = db.Column(db.Integer, comment='用户ID')
    role_id = db.Column(db.Integer, comment='角色ID')


class SystemRolePermissionRelation(Base):
    __tablename__ = 'system_role_permission_relation'

    role_id = db.Column(db.Integer, comment='角色ID')
    permission_id = db.Column(db.Integer, comment='权限ID')


class SystemDepartment(Base):
    __tablename__ = 'system_department'

    department_name = db.Column(db.String(128), comment='部门名称')
    pid = db.Column(db.Integer, comment='上级部门ID')
    desc = db.Column(db.String(255), comment='部门描述')
    users = db.relationship('SystemUser', backref='system_department')
